package com.yanfan.zutai.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.domain.entity.SysDept;
import com.yanfan.common.core.domain.entity.SysUser;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.common.utils.StringUtils;
import com.yanfan.system.mapper.SysDeptMapper;
import com.yanfan.system.service.ISysUserService;
import com.yanfan.zutai.domain.BVueComponent;
import com.yanfan.zutai.mapper.BVueComponentMapper;
import com.yanfan.zutai.service.IBVueComponentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.yanfan.common.utils.SecurityUtils.getLoginUser;

/**
 * 自定义组件Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-25
 */
@Service
public class BVueComponentServiceImpl implements IBVueComponentService {
    @Autowired
    private BVueComponentMapper bVueComponentMapper;
    @Resource
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询自定义组件
     *
     * @param id 自定义组件主键
     * @return 自定义组件
     */
    @Override
    public BVueComponent selectBVueComponentById(Long id) {
        return bVueComponentMapper.selectBVueComponentById(id);
    }

    /**
     * 查询自定义组件列表
     *
     * @param bVueComponent 自定义组件
     * @return 自定义组件
     */
    @Override
    public List<BVueComponent> selectBVueComponentList(BVueComponent bVueComponent) {
        return bVueComponentMapper.selectBVueComponentList(bVueComponent);
    }

    /**
     * 新增自定义组件
     *
     * @param bVueComponent 自定义组件
     * @return 结果
     */
    @Override
    public int insertBVueComponent(BVueComponent bVueComponent) {
        Snowflake snowflake = IdUtil.getSnowflake(6, 2);
        long id = snowflake.nextId();
        bVueComponent.setId(id);
        bVueComponent.setCreateTime(DateUtils.getNowDate());
        int i = bVueComponentMapper.insertBVueComponent(bVueComponent);
        SysUser user = getLoginUser().getUser();
        if (!user.isAdmin() && user.getUserId()!=1) {
            bVueComponent.setUserId(user.getUserId());
            bVueComponent.setTenantId(user.getDeptId());
            bVueComponent.setTenantName(user.getUserName());
            bVueComponentMapper.addbVueComponentUser(bVueComponent);
        }
        return i;
    }

    /**
     * 修改自定义组件
     *
     * @param bVueComponent 自定义组件
     * @return 结果
     */
    @Override
    public int updateBVueComponent(BVueComponent bVueComponent) {
        bVueComponent.setUpdateTime(DateUtils.getNowDate());
        return bVueComponentMapper.updateBVueComponent(bVueComponent);
    }

    /**
     * 批量删除自定义组件
     *
     * @param ids 需要删除的自定义组件主键
     * @return 结果
     */
    @Override
    public int deleteBVueComponentByIds(Long[] ids) {
        return bVueComponentMapper.deleteBVueComponentByIds(ids);
    }

    /**
     * 删除自定义组件信息
     *
     * @param id 自定义组件主键
     * @return 结果
     */
    @Override
    public int deleteBVueComponentById(Long id) {
        return bVueComponentMapper.deleteBVueComponentById(id);
    }

    @Override
    public AjaxResult fpzujiantUser(Long deptId, String zujianId) {
        SysDept sysDept = sysDeptMapper.selectDeptById(deptId);
        if (null == sysDept || null == sysDept.getDeptUserId()) {
            return AjaxResult.error("机构不存在或未绑定管理员，请调整后重试！");
        }
        Long deptUserId = sysDept.getDeptUserId();
        SysUser sysUser = userService.selectUserById(deptUserId);
        if (null == sysUser) {
            return AjaxResult.error("机构管理员不存在");
        }
        List<String> zujianIdList = StringUtils.str2List(zujianId, ",", true, true);
        // 使用 Stream API 将字符串列表转换为长整型列表
        List<Long> zujianIdLongList = zujianIdList.stream().map(Long::parseLong)  // 将每个字符串转换为 long 类型
                .collect(Collectors.toList());  // 收集结果到列表
        int flag = bVueComponentMapper.updatezujianUserlist(deptId, sysUser.getUserName(), zujianIdLongList);
        return flag > 0 ? AjaxResult.success("分配图表成功") : AjaxResult.error("分配图表失败");
    }

}
